<?php

namespace app\admin\controller\content;

use app\admin\controller\Controller;
use app\admin\model\article\Article as ArticleModel;

/**
 * 标签管理控制器
 */
class Article extends Controller{
    /**
     * 标签列表
     */
    public function index(){
        $model = new ArticleModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加标签
     */
    public function add(){
        $model = new ArticleModel;
        if(!$this->request->isAjax()){
            return $this->fetch('add');
        }
        // 新增记录
        if($model->add($this->postData('article'))) {
            return $this->renderSuccess('添加成功',url('content.article/index'));
        }
        return $this->renderError($model->getError()?:'添加失败');
    }

    /**
     * 更新标签
     */
    public function edit($article_id){
        //标签详情
        $model=ArticleModel::detail($article_id);
        if(!$this->request->isAjax()){
            return $this->fetch('edit',compact('model'));
        }
        //更新记录
        if($model->edit($this->postData('article'))){
            return $this->renderSuccess('更新成功',url('content.article/index'));
        }
        return $this->renderError($model->getError()?:'更新失败');
    }

    /**
     * 删除标签
     */
    public function delete($article_id){
        //标签详情
        $model=ArticleModel::detail($article_id);
        if(!$model->setDelete(1)){
            return $this->renderError($model->getError()?:'删除失败');
        }
        return $this->renderSuccess('删除成功',url('content.article/index'));
    }

    /**
     * 恢复标签
     */
    public function huifu($article_id){
        //标签详情
        $model=ArticleModel::detail($article_id);
        if(!$model->setDelete(0)){
            return $this->renderError($model->getError()?:'恢复失败');
        }
        return $this->renderSuccess('恢复成功',url('content.article/index'));
    }

}